package mvc;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import org.geom.Point2D;
import org.geom.Vector3D;
import org.geom.render.ViewJOGL;


public class ControllerMouse3D 
implements MouseListener, MouseMotionListener, KeyListener{

	public static boolean mousePressed = false;

	protected enum Action { ROTATE, TRANSLATE, SCALE };
	protected Action action = Action.TRANSLATE;

	protected Point2D mouseDown;// = new Point2D();
	protected int deltax;
	protected int deltay;

	protected ViewJOGL view;

	private Vector3D pre;
	
	public ControllerMouse3D(ViewJOGL view3d) {
		this.view = view3d;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		
		deltax = e.getX() - (int)mouseDown.x;
		deltay = e.getY() - (int)mouseDown.y;

		mouseMoved(e);

		switch(action) {
		case SCALE: 
			view.setScale(new Vector3D(deltay*0.01f, 0, 0));
			break;
		case TRANSLATE:
			view.setTranslate(new Vector3D( (pre.x + deltax), (pre.y + deltay), 0));
			break;
		case ROTATE:
			view.setRotate(new Vector3D(deltax*0.9f, deltay*0.9f, 0));
			break;
		}

		view.render();
	}

	
	@Override
	public void keyPressed(KeyEvent e) {
		char c = e.getKeyChar();
		System.out.println("ControllerMouse3D > keyPressed > "+c);
		if ( c=='t' || c=='T') action = Action.TRANSLATE;
		if ( c=='r' || c=='R') action = Action.ROTATE;
		if ( c=='s' || c=='S') action = Action.SCALE;
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	
	@Override
	public void mouseMoved(MouseEvent e) {
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		view.setFocusable(true);
		view.requestFocus();
		
		mouseDown = new Point2D(e.getX(), e.getY());
		pre = view.getTranslate();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


}